package com.demo.function.shardingsphere.config;

import com.google.common.collect.Lists;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 分片常量对象
 * 因为分库策略对象不是Spring容器管理的Bean，所以通过配置的变量注入到静态常量里
 * @author K
 */
@Component
public class ShardingConstant {

    /**
     * 分库数量
     */
    public static Integer DB_NUM;

    /**
     * 数据库名称
     */
    public static String DB_NAME;

    public static List<String> COLUMNS;

    @Value("${sharding.database.num}")
    public void setDbNum(int dbNum) {
        ShardingConstant.DB_NUM = dbNum;
    }

    @Value("${sharding.database.name}")
    public void setDbName(String dbName) {
        ShardingConstant.DB_NAME = dbName;
    }

    @Value("${sharding.database.columns}")
    public void setDbColumns(String columns) {
        ShardingConstant.COLUMNS = Lists.newArrayList(StringUtils.split(columns, ",", true));
    }
}
